package com.zdm.controller;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

/**
 * @author sf
 * @date 2019/6/24.
 * 统一线程池使用
 */
@Configuration
public class ThreadConfig {
    /**
     * 核心线程数
     */
    private static int corePoolSize = 5;
    /**
     * 最大线程数
     */
    private static int maximumPoolSize = 20;
    /**
     * 线程生存周期
     */
    private static int keepAliveTime = 5;

    private static int capacity = 9999;

    @Bean
    public ThreadPoolExecutor poolExecutor() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("threadPool-runner-%d")
                .setUncaughtExceptionHandler((Thread t, Throwable e) -> {
                    if (e.getMessage().contains("sendCustomerSocketMsg")) {
                        return;
                    }
                    e.printStackTrace();
                }).build();
        return new ThreadPoolExecutor(corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                threadFactory);
    }
}
